import React,{useState,useEffect} from "react";
import {  ScrollView,Text,View,Pressable,Image,Clipboard} from 'react-native';
import Headers from '@/app/components/header/Headers';
import {size,loginOut} from '@/app/utils';
import Button from '@/app/components/Button'
import { useNavigation } from "@react-navigation/native";
import { styles } from "@/app/style/home/HomeStyle";
import { useTranslation } from '@/app/utils/useTranslation';
import { getUserInfo } from './serve'
import {getRenZhengInfoApi} from './serve'
import { useSafeAreaInsets } from 'react-native-safe-area-context';
let lv0 = require('@/assets/images/lv/2.png')
let lv1 = require('@/assets/images/lv/1.png')
let lv2 = require('@/assets/images/lv/2.png')
let lv3 = require('@/assets/images/lv/3.png')
let lv4 = require('@/assets/images/lv/4.png')
let lv5 = require('@/assets/images/lv/5.png')
let lv6 = require('@/assets/images/lv/6.png')
let lv7 = require('@/assets/images/lv/7.png')
// const { RestartModule } = NativeModules;

const UserInfoScreen = (props:any) => {
    const { t } = useTranslation();
    const insets = useSafeAreaInsets();
    let [opciaty,setOpciaty] = useState(0);
    let [disabled,setDisabled] = useState(true);
    let [uid,setUid] = useState(null);
    let [isRenZheng,setIsRenZheng] = useState(0);
    let [userInfo,setUserInfo] = useState({
        loginName:null,
        lv:0,
        lvImg:null,
    });
    let navigation = useNavigation<any>();

    useEffect(()=>{
        getUserInfo({}).then((res)=>{
            setDisabled(false);
            // let data = res.data||{};
            if(res && res.data){
                let  userInfoDaya = res.data;
                if(userInfoDaya.level+''=='0'){
                    userInfoDaya.lvImg = null;
                }else if(userInfoDaya.level+''=='1'){
                    userInfoDaya.lvImg = lv1;
                }else if(userInfoDaya.level+''=='2'){
                    userInfoDaya.lvImg = lv2;
                }else if(userInfoDaya.level+''=='3'){
                    userInfoDaya.lvImg = lv3;
                }else if(userInfoDaya.level+''=='4'){
                    userInfoDaya.lvImg = lv4;
                }else if(userInfoDaya.level+''=='5'){
                    userInfoDaya.lvImg = lv5;
                }else if(userInfoDaya.level+''=='6'){
                    userInfoDaya.lvImg = lv6;
                }else if(userInfoDaya.level+''=='7'){
                    userInfoDaya.lvImg = lv7;
                }
                setUid(userInfoDaya.id);
                setUserInfo(userInfoDaya);
                getRenZhengInfo();
            }
        }).catch(()=>{
            setDisabled(false);
        })
    },[])

    const getRenZhengInfo=()=>{
        getRenZhengInfoApi({}).then(res=>{
            let data = res.data?res.data:{};
            setDisabled(false);
            if(data.status=='create'){
                setIsRenZheng(1)
            }else if(data.status=='yes'){
                setIsRenZheng(2)
            }else{
                setIsRenZheng(0)
            }
        })
    }

    // 触发重启
    // const handleRestart = () => {
    //     RestartModule.restartApp();
    // };

    return(
        <View style={{ flex: 1,flexDirection:'column',backgroundColor:'#fff'}}>
            <Headers
                title={''}
                border={false}
                barStyle={0}
                leftIcon={require('@/assets/images/icon/back.png')}
                rightIcon={require('@/assets/images/home/2.png')}
                leftTitle={''}
                backgroundColor={`rgba(255,255,255,${opciaty})`}
                centerColor={'#666'}
                {...props}
            />
            <View style={{flex: 1,}}>
                <ScrollView 
                    onScroll = {(event)=>{{
                        if(event.nativeEvent.contentOffset.y>90){
                            setOpciaty(1);
                        }else{
                            setOpciaty(event.nativeEvent.contentOffset.y/90);
                        }
                    }}}
                >
                    <View style={{paddingLeft:15,paddingRight:15}}>
                        <View style={styles.choutitouxiang}>
                            <View style={styles.choutitouxiangboxgroup}>
                                <Image style={styles.choutitouxiangbox} source={require('@/assets/images/home/1.png')}/>
                                {userInfo.lvImg?<View style={styles.lvimgbox}>
                                    <Image style={styles.lvimg} source={userInfo.lvImg}/>
                                </View>:null}
                            </View>
                            {disabled?<View style={{height:size(20),backgroundColor:'#f9f9f9',marginTop:size(10),width:size(140)}}></View>:<View style={{flexDirection:'row',alignItems:'center',marginTop:size(10),height:size(20)}}>
                                <Text style={{color:'#999',fontSize:size(14),fontWeight:'500'}}>UID:{uid}</Text>
                                <Pressable onPress={()=>{
                                     Clipboard.setString(uid+'');
                                     global.$toast({message:t('t46')})
                                }}><Text style={{color:'#38B781',marginLeft:size(6),fontSize:size(14)}}>{t('t47')}</Text></Pressable>
                            </View>}
                            <Text numberOfLines={1} style={styles.choutinametext}>{userInfo.loginName?userInfo.loginName:''}</Text>
                            {disabled?<View style={{height:size(30),backgroundColor:'#f9f9f9',marginTop:size(10),width:size(140),borderRadius:size(40)}}></View>:(isRenZheng+''=='1'||isRenZheng+''=='2')?<Pressable onPress={()=>{
                                 props.navigation.navigate('idCardAuthStatus')
                            }} style={styles.renzhengcardbox}>
                                <Image style={styles.renzhengcardimg} source={require('@/assets/images/home/12.png')}/>
                                <View>
                                    {/* 已验证身份 */}
                                    <Text style={{color:"#38B781"}}>{isRenZheng+''=='1'?t('t350'):t('t281')}</Text>
                                </View>
                            </Pressable>:<Pressable onPress={()=>{
                                props.navigation.navigate('idCardAuth')
                            }} style={[styles.renzhengcardbox,styles.renzhengedcardbox]}>
                                <Image style={styles.renzhengcardimg} source={require('@/assets/images/home/16.png')}/>
                                <View>
                                    {/* 未认证 */}
                                    <Text style={{color:"#767676"}}>{t('t352')}</Text>
                                </View>
                            </Pressable>}
                        </View>
                        <View style={styles.choutimenubox}>
                            <Pressable onPress={()=>{
                                navigation.navigate('pages/shareApp/ShareAppScreen')
                             }}  style={styles.choutimenuitembox}>
                                <Image style={styles.choutimenuitemimg} source={require('@/assets/images/home/13.png')}/>
                                <Text style={styles.choutimenuitemimgtext} numberOfLines={1}>{t('t353')}</Text>
                            </Pressable>
                             <Pressable onPress={()=>{
                                props.navigation.navigate('changeEmail')
                             }} style={styles.choutimenuitembox}>
                                <Image style={styles.choutimenuitemimg} source={require('@/assets/images/home/14.png')}/>
                                <Text style={styles.choutimenuitemimgtext} numberOfLines={1}>{t('t118')}</Text>
                            </Pressable>
                             <Pressable onPress={()=>{
                                props.navigation.navigate('changePassword')
                             }}  style={styles.choutimenuitembox}>
                                <Image style={styles.choutimenuitemimg} source={require('@/assets/images/home/15.png')}/>
                                <Text style={styles.choutimenuitemimgtext} numberOfLines={1}>{t('t134')}</Text>
                            </Pressable>
                        </View>
                        <View>
                             <Pressable onPress={()=>{
                                navigation.navigate('pages/langeuageSettingPage/langSetting')
                             }} style={styles.choutilistitembox}>
                                <View>
                                    <Text style={styles.choutilistitemtextbox}>{t('t354')}</Text>
                                </View>
                                <Image
                                    style={styles.noticeboxrightimg}
                                    source={
                                        require('@/assets/images/home/11.png')
                                    }
                                />
                            </Pressable>
                            <Pressable onPress={()=>{
                                navigation.navigate('pages/AboutmreScreen/AboutmreScreen')
                             }}  style={styles.choutilistitembox}>
                                <View>
                                    <Text style={styles.choutilistitemtextbox}>{t('t10')}</Text>
                                </View>
                                <Image
                                    style={styles.noticeboxrightimg}
                                    source={
                                        require('@/assets/images/home/11.png')
                                    }
                                />
                            </Pressable>
                            {/* <Pressable onPress={()=>{
                                handleRestart();
                             }}  style={styles.choutilistitembox}>
                                <View>
                                    <Text style={styles.choutilistitemtextbox}>重启应用</Text>
                                </View>
                                <Image
                                    style={styles.noticeboxrightimg}
                                    source={
                                        require('@/assets/images/home/11.png')
                                    }
                                />
                            </Pressable> */}
                        </View>
                    </View>
                </ScrollView>
                <View style={[styles.choutibtngroupbox,{paddingBottom:size(insets.bottom?insets.bottom:10)}]}>
                    <Button 
                        onClick={() => {
                            global.$dialog({
                                title:t('t356'),
                                content:t('t357'),
                                callback: () => {
                                    loginOut();
                                } //确定按钮要做的事件
                            }) // 弹框提示
                        }}
                            style={styles.login_box_footer_btn}
                    >
                        <Text style={styles.login_box_footer_btn_text}>{t("LoginOut")}</Text>
                    </Button>
                </View>
            </View>
        </View>
    )
}

export default UserInfoScreen;